var pageii = null;
var grid = null;
$(function () {
    grid = lyGrid({
        id: 'paging',
        l_column: [{
            colkey: "id",
            name: "id",
            hide: true
        }, {
            colkey: "parentId",
            name: "parentId",
            hide: true
        }, {
            colkey: "formMap.phone",
            name: "用户",
            width: "300px",
            renderData: function (rowindex, data, rowdata, column) {
                return rowdata.formMap.nickname + '(' + rowdata.formMap.phone + ')';
            }
        }, {
            colkey: "formMap.name",
            name: "姓名"
        }, {
            colkey: "formMap.code",
            name: "用户编码"
        }, {
            colkey: "formMap.agent_name",
            name: "机构商"
        }, {
            colkey: "formMap.time",
            name: "加入时间"
        }, {
            colkey: "formMap.isreal",
            name: "实名认证",
            renderData: function (rowindex, data, rowdata, column) {
                if (data == "0") {
                    return "<span style='color: red;'>否</span>";
                } else if (data == "1") {
                    return "是";
                }
            }
        }, {
            colkey: "formMap.status",
            name: "用户状态",
            renderData: function (rowindex, data, rowdata, column) {
                var str = '';
                if (data == "0") {
                    str = "正常";
                } else if (data == "1") {
                    str = "<span style='color: red;'>停用</span>";
                }
                var id = rowdata.formMap.id;
                var status = rowdata.formMap.status;
                return "<a href='javascript:toggleStatus(" + id + "," + status + ");'>" + str + "</a>";
            }
        }, {
            colkey: "formMap.status_income",
            name: "分润状态",
            renderData: function (rowindex, data, rowdata, column) {
                var str = '';
                if (data == "0") {
                    str = "正常";
                } else if (data == "1") {
                    str = "<span style='color: red;'>停用</span>";
                }
                var id = rowdata.formMap.id;
                var status = rowdata.formMap.status_income;
                return "<a href='javascript:toggleStatusIncome(" + id + "," + status + ");'>" + str + "</a>";
            }
        }, {
            colkey: "formMap.status_withdrawal",
            name: "提现状态",
            renderData: function (rowindex, data, rowdata, column) {
                var str = '';
                if (data == "0") {
                    str = "正常";
                } else if (data == "1") {
                    str = "<span style='color: red;'>停用</span>";
                }
                var id = rowdata.formMap.id;
                var status = rowdata.formMap.status_withdrawal;
                return "<a href='javascript:toggleStatusWithdrawal(" + id + "," + status + ");'>" + str + "</a>";
            }
        }/*, {
            colkey: "formMap.locked",
            name: "锁定状态",
            renderData: function (rowindex, data, rowdata, column) {
                var str = '';
                if (data == "0") {
                    str = "正常";
                } else if (data == "1") {
                    str = "<span style='color: red;'>锁定</span>";
                }
                var id = rowdata.formMap.id;
                var status = rowdata.formMap.locked;
                return "<a href='javascript:toggleLocked(" + id + "," + status + ");'>" + str + "</a>";
            }
        }*/],
        jsonUrl: rootPath + '/star/user/treelists.shtml',
        checkbox: true,
        usePage: false,
        records: "treelists",
        treeGrid: {
            type: 1,
            tree: true,
            hide: false,
            name: 'formMap.phone',
            id: "id",
            pid: "parentId"
        }
    });

    $("#search").click("click", function () { // 绑定查询按扭
        var searchParams = $("#searchForm").serializeJson();
        grid.setOptions({
            data: searchParams
        });
    });
});

function toggleStatus(id, status) {
    layer.confirm('是否执行该操作？', function (index) {
        if (status == 1) {
            status = 0;
        } else if (status == 0) {
            status = 1;
        }
        var url = rootPath + '/star/user/editStatus.shtml';
        var s = CommnUtil.ajax(url, {
            id: id,
            status: status
        }, "json");
        if (s == "success") {
            layer.msg('成功');
            grid.loadData();
        } else {
            layer.msg('失败');
        }
    });
}

function toggleStatusIncome(id, status) {
    layer.confirm('是否执行该操作？', function (index) {
        if (status == 1) {
            status = 0;
        } else if (status == 0) {
            status = 1;
        }
        var url = rootPath + '/star/user/editStatusIncome.shtml';
        var s = CommnUtil.ajax(url, {
            id: id,
            status: status
        }, "json");
        if (s == "success") {
            layer.msg('成功');
            grid.loadData();
        } else {
            layer.msg('失败');
        }
    });
}

function toggleStatusWithdrawal(id, status) {
    layer.confirm('是否执行该操作？', function (index) {
        if (status == 1) {
            status = 0;
        } else if (status == 0) {
            status = 1;
        }
        var url = rootPath + '/star/user/editStatusWithdrawal.shtml';
        var s = CommnUtil.ajax(url, {
            id: id,
            status: status
        }, "json");
        if (s == "success") {
            layer.msg('成功');
            grid.loadData();
        } else {
            layer.msg('失败');
        }
    });
}

function toggleLocked(id, status) {
    layer.confirm('是否执行该操作？', function (index) {
        if (status == 1) {
            status = 0;
        } else if (status == 0) {
            status = 1;
        }
        var url = rootPath + '/star/user/editLocked.shtml';
        var s = CommnUtil.ajax(url, {
            id: id,
            status: status
        }, "json");
        if (s == "success") {
            layer.msg('成功');
            grid.loadData();
        } else {
            layer.msg('失败');
        }
    });
}
